package com.hslxy.niukewang.config;

import com.hslxy.niukewang.quartz.AlphaJob;
import com.hslxy.niukewang.quartz.PostScoreRefreshJob;
import org.quartz.JobDataMap;
import org.quartz.JobDetail;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.quartz.JobDetailFactoryBean;
import org.springframework.scheduling.quartz.SimpleTriggerFactoryBean;

//配置 -> 数据库 -> 调用
@Configuration
public class QuartzConfig {

//  配置JobDetail
//    @Bean
//    public JobDetailFactoryBean jobDetailFactoryBean(){
//        JobDetailFactoryBean jobDetailFactoryBean = new JobDetailFactoryBean();
//        jobDetailFactoryBean.setJobClass(AlphaJob.class);
//        jobDetailFactoryBean.setName("alphaJob");
//        jobDetailFactoryBean.setGroup("alphaJobGroup");
//        jobDetailFactoryBean.setRequestsRecovery(true);
//        jobDetailFactoryBean.setDurability(true);
//        return jobDetailFactoryBean;
//    }
//    //  配置Trigger
//    @Bean
//    public SimpleTriggerFactoryBean alphaTrigger(JobDetail jobDetailFactoryBean){
//        SimpleTriggerFactoryBean factoryBean = new SimpleTriggerFactoryBean();
//        factoryBean.setJobDetail(jobDetailFactoryBean);
//        factoryBean.setName("alphaTrigger");
//        factoryBean.setGroup("alphaTriggerGroup");
//        factoryBean.setRepeatInterval(3000);
//        factoryBean.setJobDataMap(new JobDataMap());
//        return factoryBean;
//    }

//刷新帖子计算分数
    @Bean
    public JobDetailFactoryBean scoreRefreshJobDetail(){
        JobDetailFactoryBean jobDetailFactoryBean = new JobDetailFactoryBean();
        jobDetailFactoryBean.setJobClass(PostScoreRefreshJob.class);
        jobDetailFactoryBean.setName("postScoreRefreshJob");
        jobDetailFactoryBean.setGroup("postScoreRefreshJobGroup");
        jobDetailFactoryBean.setRequestsRecovery(true);
        jobDetailFactoryBean.setDurability(true);
        return jobDetailFactoryBean;
    }
    //  配置Trigger
    @Bean
    public SimpleTriggerFactoryBean scoreRefreshTrigger(JobDetail scoreRefreshJobDetail){
        SimpleTriggerFactoryBean factoryBean = new SimpleTriggerFactoryBean();
        factoryBean.setJobDetail(scoreRefreshJobDetail);
        factoryBean.setName("postScoreTrigger");
        factoryBean.setGroup("postScoreTriggerGroup");
        factoryBean.setRepeatInterval(1000 * 60 * 5);
        factoryBean.setJobDataMap(new JobDataMap());
        return factoryBean;
    }
}
